package com.vhall.component.controller.gift;

import com.alibaba.fastjson.JSON;
import com.vhall.component.entity.gift.dto.GiftListDataDTO;
import com.vhall.component.entity.gift.dto.GiftPageDTO;
import com.vhall.component.entity.gift.dto.RoomGiftDTO;
import com.vhall.component.entity.gift.vo.*;
import com.vhall.component.entity.page.vo.PageVO;
import com.vhall.component.entity.room.dto.RoomsEntityDTO;
import com.vhall.component.framework.common.annotation.VhallComponent;
import com.vhall.component.framework.common.exception.BusinessException;
import com.vhall.component.plugin.common.annotation.TokenAuth;
import com.vhall.component.plugin.common.annotation.VssTokenAuth;
import com.vhall.component.plugin.common.exception.BizErrorCode;
import com.vhall.component.plugin.common.support.Token;
import com.vhall.component.plugin.common.support.TokenThreadLocal;
import com.vhall.component.plugin.common.support.VssToken;
import com.vhall.component.plugin.common.support.VssTokenThreadLocal;
import com.vhall.component.plugin.core.enums.SendMessageTypeEnum;
import com.vhall.component.plugin.core.service.PaasService;
import com.vhall.component.service.gift.GiftService;
import com.vhall.component.service.gift.common.ServiceCode;
import com.vhall.component.service.room.RoomInnerService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashMap;
import java.util.Map;

/**
 * @author xianglei
 * @date 2021/7/13 14:02
 */
@VhallComponent
public class GiftController implements GiftFacade {

    @Autowired
    private GiftService giftService;

    @Autowired
    private PaasService paasService;

    @Autowired
    private RoomInnerService roomInnerService;

    @VssTokenAuth
    @Override
    public GiftRspVo create(GiftAddReqVo reqVo) {
        reqVo.setSourceType(null == reqVo.getSourceType() ? 0 : reqVo.getSourceType());
        reqVo.setSourceStatus(null == reqVo.getSourceStatus() ? 0 : reqVo.getSourceStatus());
        return giftService.create(reqVo);
    }

    @VssTokenAuth
    @Override
    public Integer delete(GiftDelReqVo reqVo) {
        Integer delete = giftService.delete(reqVo);
        if (delete <= 0) {
            throw new BusinessException(BizErrorCode.COMP_GIFT_DELETE_FAILURE);
        }
        return delete;
    }

    @VssTokenAuth
    @Override
    public GiftRspVo update(GiftUpdateReqVo reqVO) {
        return giftService.update(reqVO);
    }

    @VssTokenAuth
    @Override
    public Map<String, Object> list(ListReqVo reqVo) {
        return giftService.list(reqVo);
    }

    @VssTokenAuth
    @Override
    public GiftPageDTO<RoomGiftDTO> roomGiftList(RoomGiftListVO vo) {
        return giftService.roomGiftList(vo);
    }

    @Override
    @TokenAuth
    public PageVO<GiftListDataDTO> watchGiftList(WatchGiftListReqVo reqVo) {
        return giftService.watchGiftList(reqVo);
    }

    @VssTokenAuth
    @Override
    public MapSaveRspVO mappingSave(MapSaveReqVo reqVo) {
        return giftService.mappingSave(reqVo);
    }


    @Override
    public PayStatusSetRspVO setPayStatus(SetPayStatusReqVo reqVo) {
        return giftService.setPayStatus(reqVo);
    }

    @VssTokenAuth
    @Override
    public GiftSendRspVo send(GiftSendReqVo reqVo) {
        if (ServiceCode.JSAPI.equals(reqVo.getServiceCode()) && StringUtils.isBlank(reqVo.getOpenId())) {
            throw new BusinessException(BizErrorCode.BIZ_INVALID_PARAM);
        }
        VssToken token = VssTokenThreadLocal.getToken();
        reqVo.setGiftUserId(String.valueOf(token.getThirdPartyUserId()));
        return giftService.send(reqVo);
    }

    @Override
    @TokenAuth
    public Map<String, String> newSend(GiftNewSendReqVo reqVo) {
        Token token = TokenThreadLocal.getToken();

        Long giftId = reqVo.getGiftId();
        GiftListDataDTO info = giftService.getInfoById(giftId);

        RoomsEntityDTO roomInfo = roomInnerService.getRoomInfo(reqVo.getRoomId());
        if (roomInfo == null) {
            throw new BusinessException(BizErrorCode.EMPTY_ROOM);
        }

        HashMap<String, Object> map = new HashMap<>(8);
        map.put("type", SendMessageTypeEnum.GIFT_SEND_SUCCESS.getType());
        map.put("room_id", reqVo.getRoomId());
        map.put("gift_user_nickname", token.getNickname());
        map.put("gift_name", info.getName());
        map.put("gift_image_url", info.getEffectImg());
        map.put("gift_numbers", reqVo.getNumbers());
        map.put("gift_user_id", token.getAccountId());
        try {
            paasService.sendMessage(roomInfo.getChannelId(), JSON.toJSONString(map));
        } catch (Exception e) {
            throw new BusinessException(BizErrorCode.COMP_GIFT_SEND_FAILURE);
        }
        return new HashMap<>(1);
    }

    @VssTokenAuth
    @Override
    public Map<String, Object> useList(UseListReqVo reqVo) {
        return giftService.useList(reqVo);
    }

}
